/*
 * Copyright 2007-2008. Adobe Systems Incorporated.
 * All rights reserved.
 */

var SALE; if (!SALE) SALE = {};
if (!SALE.Utils) SALE.Utils = {};

SALE.Utils = {
	
	createHTMLLoader : function(path, callback) {
		var loader = new air.HTMLLoader();
		loader.x = 20;
		loader.y = 20;
		loader.paintsDefaultBackground = false;
		//
		loader.filters = [SALE.Utils.createShadowFilter()];
		loader.addEventListener(air.Event.COMPLETE, callback);
		loader.load(new air.URLRequest(path));
		//
		return loader;
	},
	
	createShadowFilter : function (){
		var shadowFilter = new runtime.flash.filters.DropShadowFilter(0, 0);
		shadowFilter.blurX = 20;
		shadowFilter.blurY = 20;
		return shadowFilter; 
	},
	
	createRecentlyDateJson : function() {
		if(!this.recentlyDateJson) {
			var today = new Date();
			this.recentlyDateJson = {
				dates: []
			};
			for (var i = 0; i < 31; i++) {
				var date = today.getDate();
				var newdate = new Date();
				newdate.setDate(date - i);
				var dateRecord = {
					name: (newdate.getMonth() + 1 ) + "月" + newdate.getDate() + "日",
					value: newdate
				}
				this.recentlyDateJson.dates.push(dateRecord);
			}
		}
		return this.recentlyDateJson;
	},
	
	createRecentlyMonthJson : function() {
		if(!this.recentlyMonthJson) {
			var today = new Date();
				this.recentlyMonthJson = {
					monthes: []
				};
				for (var i = 0; i < 12; i++) {
					var month = today.getMonth();
					var newdate = new Date();
					newdate.setDate(1);
					newdate.setMonth(month - i);
					var record = {
						name:  newdate.getFullYear() + "年" + (newdate.getMonth() + 1 ) + "月",
						value: newdate
					}
					this.recentlyMonthJson.monthes.push(record);
				}
		}
		return this.recentlyMonthJson;
	},
	
	createBillCode : function(type) {
		var codeDao = SALE.App.daoFactory.getBillCodeDao();
		var code = codeDao.createBillCode(type);
		
		return code;
	},
	
	stockRenderer : function(val,p,r) {
		var lower = r.data["lower_limit"] == null || r.data["lower_limit"] == "" ? 0 : r.data["lower_limit"];
		var upper = r.data["upper_limit"] == null || r.data["upper_limit"] == "" ? 9999999999 : r.data["upper_limit"];
		if(val == null || val == "") {
			val = 0;
		}
		if(parseInt(val) > parseInt(upper) || parseInt(val) <= parseInt(lower)){
			return '<span style="color:red;">' + val + '</span>';
		}else {
			return '<span style="color:green;">' + val + '</span>';
		}
		return val;
	},
	
	stocksStatusRenderer : function(val,p,r) {
		
		if(r.data["type"] == '1') {
			return '<span style="color:red;">入库</span>';
		} else {
			return '<span style="color:red;">出库</span>';
		}
		
	},
	
	moneyStatusRenderer : function(val,p,r) {
		
		if(r.data["type"] == '1') {
			return '<span style="color:red;">入帐</span>';
		} else {
			return '<span style="color:red;">出帐</span>';
		}
		
	},
	
	statusRenderer : function(val,p,r) {
		
		if(!r.data["confirm_status"]) {
			return '<span style="color:red;">未确认</span>';
		}
		
		if(r.data["deliveried_status"] != null && r.data["deliveried_status"] ==false) {
			return '<span style="color:red;">未出货</span>';
		}
		
		if(r.data["receive_status"] != null && r.data["receive_status"] ==false) {
			return '<span style="color:red;">未入库</span>';
		}
		
		if(!r.data["ledger_status"]) {
			return '<span style="color:red;">未入账</span>';
		}
		
		return "已完结";
	},
	
	confirmStatusRenderer : function(val,p,r) {
		
		if(!r.data["confirm_status"]) {
			return '<span style="color:red;">未确认</span>';
		}
		
		return "已确认";
	},
	
	ledgerTypeRenderer : function(val,p,r) {
		
		if(r.data["type"] == '0') {
			return '销售收入';
		}
		
		if(r.data["type"] == '1') {
			return '采购支出';
		}
		
		if(r.data["type"] == '2') {
			return '其它收入';
		}
		
		if(r.data["type"] == '3') {
			return '一般费用';
		}
	},
	
	ledgerStatusRenderer : function(val,p,r) {
		
		if(r.data["status"] == '1') {
			return '已结账';
		} else {
			return '<span style="color:red;">未结账</span>';
		}
	},
	
	starRenderer : function(val,p,record,rowIndex) {
		
		if(rowIndex == 0) {
			return '<img src="'+SALE.img.AWARD_STAR_GOLD_URL+'"/>';
		}
		
		if(rowIndex == 1) {
			return '<img src="'+SALE.img.AWARD_STAR_SILVER_URL+'"/>';
		}
		
		if(rowIndex == 2) {
			return '<img src="'+SALE.img.AWARD_STAR_BRONZE_URL+'"/>';
		}
		
		return '<img src="'+SALE.img.AWARD_STAR_TAG_URL+'"/>';
	},
	
	showMessageBox : function(message, title) {
		
		Ext.MessageBox.show({
           	title: title ? title : '提示信息',
           	msg: message,
          	buttons: Ext.MessageBox.OK,
           	icon: Ext.MessageBox.INFO,
           	minWidth: 150
       	});
	},
	
	showLoadMask : function(target,config) {
		if(target) {
			this.loadMask = new Ext.LoadMask(target.getEl(), config ? config : {msg:"请等待..."});
			this.loadMask.show();
		}
	},
	
	hideLoadMask : function() {
		if(this.loadMask) {
			this.loadMask.hide();
		}
	},
	
	themes: {
		"99BBE8":"blue",
		"9FD5C2":"green",
		"9F82FF":"purple",
		"FF6363":"peppermint",
		"875546":"chocolate",
		"D0D0D0":"gray",
		"C0C0C0":"gray-extend",
		"686868":"darkgray",
		"485569":"slate",
		"3D3D62":"indigo",
		"16181A":"access"
	},
	changeTheme : function(color) {
		if(color && this.themes[color]) {
			Ext.util.CSS.swapStyleSheet("theme","lib/ext/resources/css/xtheme-" + this.themes[color] +".css");
			Ext.util.CSS.swapStyleSheet("style","lib/app/css/style-"+ this.themes[color] +".css"); 
			Ext.util.CSS.swapStyleSheet("font","lib/app/css/font.css"); 
			
			LocalXMLFileUtil.configFileUtil.saveTheme(color);
			SALE.App.configInfo["theme"] = color;
		}
	},
	
	navigateToURL : function(url) {
		var urlReq = new air.URLRequest(url); 
		air.navigateToURL(urlReq);
	}
};

SALE.Utils.result = {
		
	decode : function(response) {
		//hide the load mask
		SALE.Utils.hideLoadMask();
	
		var text = response.responseText;
		try {
			return Ext.util.JSON.decode(text);
		} catch(err) {			
			var message = Ext.util.Format.ellipsis(text,300);
			return {success:false,message:'操作失败！<br/><br/>'+message};
		}
	}
};

SALE.Utils.config = {
	
	checkServiceUrl : function(url) {
		if(url) {
			var lastchar = url.substring(url.length-1);
			
			if(lastchar != "/") {
				url += "/"
			}
		}
		return url;
	},
	
	getFileUrl : function(file) {
		if(file) {
			var paths = file.split("\\");
			var path = "attached/"+SALE.App.userInfo.company_code+"/"+paths[paths.length-1];
			return (SALE.App.configInfo.url + path);
		}
	}
};


SALE.getBounds = function(element) {
	var offset = jQuery(element).offset();
	var htmlLoader = element.ownerDocument.defaultView.htmlLoader;
	return {
		x: offset.left + htmlLoader.x,
		y: offset.top + htmlLoader.y,
		width: jQuery(element).width(),
		height: jQuery(element).height()
	}
}

// Modify standard objects\
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
String.prototype.replaceAll  = function(s1,s2){   
	return this.replace(new RegExp(s1,"gm"),s2);   
}  

Function.prototype.createCallback = function(obj, args){
		var method = this;
		return function() {
			var callArgs = args || arguments;
			return method.apply(obj || window, callArgs);
		};
};
